import moment from "moment";

export function genTime(st, et) {
  function Fill0(n) {
    if (n > 10) {
      return n;
    } else {
      return "0" + n;
    }
  }

  let x = et - st,
    H = 60 * 60 * 1000,
    M = 60 * 1000,
    S = 1000,
    h,
    m,
    s;
  h = m = s = 0;

  // d = Math.floor(x / D);
  // d > 0 && (x = x - (D * d));

  h = Math.floor(x / H);
  h > 0 && (x = x - H * h);

  m = Math.floor(x / M);
  m > 0 && (x = x - M * m);

  s = Math.floor(x / S);
  return `${Fill0(h)}:${Fill0(m)}:${Fill0(s)}`;
}

export function formatMs(data) {
  return moment(data).format("x") - 0;
}

export function formatAll(data) {
  return data ? moment(data).format("YYYY-MM-DD HH:mm:ss") : null;
}

//时间校验公用函数
export function validationTime(state = null, end = null, type) {
  const st = state && formatMs(state);
  const et = end && formatMs(end);
  if (type === "START" && end && state && st >= et) {
    return "START_ERROR";
  } else if (type === "END" && end && state && st >= et) {
    return "END_ERROR";
  }
  return "OK";
}
